package com.rent.room.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Date;
import java.util.List;

import com.rent.apartment.domain.ApartmentInfo;
import com.rent.apartment.mapper.ApartmentInfoMapper;
import com.rent.reserve.domain.Reserve;
import com.rent.reserve.mapper.ReserveMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Service;
import com.rent.room.mapper.RoomInfoMapper;
import com.rent.room.domain.RoomInfo;
import com.rent.room.service.IRoomInfoService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 房间信息Service业务层处理
 *
 * @author cooper
 * @date 2024-10-09
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo> implements IRoomInfoService {

    @Autowired
    private RoomInfoMapper roomInfoMapper;

    @Autowired
    private ReserveMapper reserveMapper;

    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;

    /**
     * 查询房间信息
     *
     * @param id 房间信息主键
     * @return 房间信息
     */
    @Override
    public RoomInfo selectRoomInfoById(Long id) {
        return roomInfoMapper.selectRoomInfoById(id);
    }

    /**
     * 查询房间信息列表
     *
     * @param roomInfo 房间信息
     * @return 房间信息
     */
    @Override
    public List<RoomInfo> selectRoomInfoList(RoomInfo roomInfo) {
        return roomInfoMapper.selectRoomInfoList(roomInfo);
    }

    /**
     * 新增房间信息
     *
     * @param roomInfo 房间信息
     * @return 结果
     */
    @Override
    public int insertRoomInfo(RoomInfo roomInfo) {
            return roomInfoMapper.insertRoomInfo(roomInfo);
    }

    /**
     * 修改房间信息
     *
     * @param roomInfo 房间信息
     * @return 结果
     */
    @Override
    public int updateRoomInfo(RoomInfo roomInfo) {
        return roomInfoMapper.updateRoomInfo(roomInfo);
    }

    /**
     * 批量删除房间信息
     *
     * @param ids 需要删除的房间信息主键
     * @return 结果
     */
    @Override
    public int deleteRoomInfoByIds(Long[] ids) {
        return roomInfoMapper.deleteRoomInfoByIds(ids);
    }

    /**
     * 删除房间信息信息
     *
     * @param id 房间信息主键
     * @return 结果
     */
    @Override
    public int deleteRoomInfoById(Long id) {
        return roomInfoMapper.deleteRoomInfoById(id);
    }

    // 10/16 预约

    @Override
    @Transactional
    public int insertReserve(Long roomId, @DateTimeFormat(pattern = "yyyy-MM-dd") Date startDate, @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate) {
        RoomInfo roomInfo = roomInfoMapper.selectRoomInfoById(roomId);
        if (roomInfo == null) {
            throw new RuntimeException("房间不存在");
        }
    
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectApartmentInfoById(roomInfo.getRoomId());
        if (apartmentInfo == null) {
            throw new RuntimeException("公寓信息不存在");
        }
    
        Reserve reserve = new Reserve();
        reserve.setApartmentId(apartmentInfo.getId());
        reserve.setHostNumber(roomInfo.getHostPhone());
        reserve.setRoomPicture(roomInfo.getRoomPicture());
        reserve.setRent(apartmentInfo.getRent());
        reserve.setStartDate(startDate);
        reserve.setEndDate(endDate);
        reserve.setStatus("ordered");
        reserve.setApartmentInfo(String.format("%s %s %s %s %s %s",
                apartmentInfo.getAddressProvince(),
                apartmentInfo.getAddressCity(),
                apartmentInfo.getAddressNeighborhood(),
                apartmentInfo.getAddressCommunity(),
                apartmentInfo.getUnit(),
                apartmentInfo.getDoorNumber()));
    
        int result = reserveMapper.insertReserve(reserve);
        if (result > 0) {
            apartmentInfoMapper.updateApartmentStatus(apartmentInfo.getId(), "ordered");
        }

        return result;
    }

    @Override
    public int updateApartmentStatus(Long apartmentId, String status) {
        return apartmentInfoMapper.updateApartmentStatus(apartmentId, status);
    }
}